package com.gsafety.wellsfort.common.util;

import java.util.Random;

public class RandomUtils {
	public static String getRandom(int length) {
		String val = "";
		Random random = new Random();
		for (int i = 0; i < length; i++) {
			val += String.valueOf(random.nextInt(10));
		}
		return val;
	}


	public static String getCode(int length){
		// 生成6位的短信验证码
		String acode = createVerificationCode(length);
		while (true) {//因为大概是短信平台的bug. 首位是0 的验证码发到手机后0没有了.所以做个验证,如果首位是0,则重新获取一下.
			if (acode.startsWith("0")) {
				acode = createVerificationCode(length);
			} else {
				break;
			}
		}
		return acode;
	}

	private static final String createVerificationCode(int verificationCodeLength) {
		// 所有候选组成验证码的字符
		String[] verificationCodeArrary = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9","A" };
		String verificationCode = "";
		Random random = new Random();
		// 利用一定范围内的随机数做为验证码数组的下标，循环组成需要长度的验证码，
		for (int i = 0; i < verificationCodeLength; i++) {
			verificationCode += verificationCodeArrary[random.nextInt(verificationCodeArrary.length)];
		}
		return verificationCode;
	}
}
